package com.yourname.custom;

import com.yourname.entity.CourseStudent;

import java.util.ArrayList;

public class CustomCourseStudentList extends ArrayList<CourseStudent> {
    @Override
    public boolean contains(Object o) {
        CourseStudent s=(CourseStudent) o;
        for (CourseStudent student:this){
            if (student.getCid().equals(s.getCid()) && student.getSid().equals(s.getSid())){
                return true;
            }
        }
        return false;
    }

    @Override
    public boolean add(CourseStudent student) {
        if (contains(student)){
            System.out.println("选课记录已存在");
            return false;
        }else{
            return super.add(student);
        }
    }

    /**
     * 根据学号查找选课记录
     * @param sid
     * @return
     */
    public CourseStudent getBySid(String sid) {
        for (CourseStudent cs:this){
            if (cs.getSid().equals(sid)){
                return cs;
            }
        }
        return null;
    }
    public CourseStudent getByCourseId(String cid) {
        for (CourseStudent cs:this){
            if (cs.getCid().equals(cid)){
                return cs;
            }
        }
        return null;
    }
    public CourseStudent getByAll(CourseStudent cos) {
        for (CourseStudent cs:this){
            if (cs.getSid().equals(cos.getSid())&&cs.getCid().equals(cos.getCid())){
                return cs;
            }
        }
        return null;
    }
}
